<?php
error_reporting(E_ALL ^ E_NOTICE^ E_WARNING);
ini_set('display_errors', '0'); 
date_default_timezone_set('Asia/Shanghai');

class MtCommon
{
    private $MT_APPID;
    private $MT_SECRET;
    private $MT_API_URL;

    public function __construct()
    {
        $this->MT_APPID = '133'; //测试环境
        $this->MT_SECRET = 'f32362d0bef2fdab699f9766794cdf87';//测试环境
        $this->MT_API_URL = 'http://test.waimaiopen.meituan.com/api/v1/';//测试环境

        // $this->MT_APPID = '133'; //正式环境
        // $this->MT_SECRET = 'c78afa438894597b6d2a31c14256d807';//正式环境
        // $this->MT_API_URL = 'http://waimaiopen.meituan.com/api/v1/';//正式环境
    }

    // 通过给定的url, params, consumer_secret计算出sig
    public function createSig($pathUrl, $params ,$consumer_secret) {
        $params = $this->concatParams($params);
        $str = $pathUrl.'?'.$params.$consumer_secret;

        return md5($str);
    }

    public function concatParams($params) {
        ksort($params);
        $fillterParams = array(
            "img_data"
        );
        $pairs = array();
        foreach($params as $key=>$val) {
            if(in_array($key, $fillterParams)){
                continue;
            }
            array_push($pairs, $key . '=' . $val);
            // array_push($pairs, $key . '=' . urlencode($val));
        }
        return join('&', $pairs);
    }

    /*
        获取公共参数
        params
            params 传输的参数
    */
    public function getPostData($params = array())
    {
        $data = array();
        $data['timestamp'] = time();
        $data['app_id'] = $this->MT_APPID;
        $res = array_merge($data,$params);

        return $res;
    }

    public function sendData($url = '', $params = array(), $type='GET')
    {
        $sig = $this->createSig($this->MT_API_URL.$url, $params ,$this->MT_SECRET);
        $url = $this->MT_API_URL.$url."?sig=".$sig."&".$this->concatParams($params);
        // var_dump($params);die;
        $ch = curl_init();
        $this_header = array(
            "Content-type: text/html; charset=utf-8"
            );
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
        switch ($type){
            case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true);break;
            case "POST": curl_setopt($ch, CURLOPT_POST,true);
                        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
            case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
            case "DELETE":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;
        }
        $return_str = curl_exec($ch);
        $return_str = json_decode($return_str, true);

        // var_dump(curl_errno($ch), curl_error($ch), $url, $return_str);
        curl_close($ch);
        return $return_str;
    }

    public function DLOG($log_content='', $log_level='', $log_name='')
    { 

        if(empty($log_level)||empty($log_content))
            return;
        
        
        if(empty($log_name) && $log_level == 'run')
            $log_name = 'run';

        $log_dir = "/mnt/logs/";
        $log_file = $log_dir.$log_name.".".date('Y-m-d').".log";
        
        //默认每行日志必写的内容，统一在这里处理
        // ZHANGXI 2015/1/7 for regular
        $time = sprintf("%8s.%03d",date('H:i:s'),floor(microtime()*1000));
        //$time = date('H:i:s').".".floor(microtime()*1000);
        $ip = sprintf("%15s",$_SERVER["REMOTE_ADDR"]);

        $id =   "req-Fn";

        $path_arr = explode("/", $_SERVER['REQUEST_URI']);

        $content_prefix = "[ ".$time." ".$ip." ".$id." ".$log_level." ".$path_arr[1]." ".$path_arr[2]." ] ";

        error_log($content_prefix.$log_content." [".getmypid()."]\n", 3, $log_file);
        return true;
    }
}